#!/usr/bin/env python
# -*- coding:utf-8 -*-

import os
import cgi
import sys
reload(sys)
sys.setdefaultencoding( "utf-8" )

def file_outputHtml(resultList, RStableName):
    """
	output the result list to the html file
	"""
	filePath = 'output/'
	fileName = RStableName + ".html"
	isFileExisted = os.path.exists(filePath+fileName)
	if (isFileExisted):
		os.unlink(filePath+fileName)
	html = open(filePath+fileName, "w")
	html.write("""<!doctype html>\n<html>\n<head>\n<meta charset="utf-8">\n<title>"""+RStableName+"""</title>\n<style type="text/css">\nbody{background-color:#f0f2f5;margin:0;padding:0 0 100px 0}ul{padding:0 5px;list-style-type:none;width:1100px;position:relative;left:50%;margin:0 0 20px -550px;border-radius:3px;background-color:#FFF;border:1px solid #d8dfe5}.title{width:100%;padding:0;left:0;height:100px;line-height:100px;margin:0 0 50px 0;border:0}.title li{border-radius:0;color:#FFF;text-align:center;font-size:36px;background-color:#ff6a5b;font-family:Cambria,"Hoefler Text","Liberation Serif",Times,"Times New Roman",serif;text-shadow:3px 3px 6px #333;border-bottom:1px solid #ce1200}li{font-size:14px;font-family:"microsoft yahei","微软雅黑";color:#999;padding:10px;background-color:#FFF;border-radius:5px}li .rowid{width:30px;display:inline-block}li .rowip{width:120px;display:inline-block}a:link{text-decoration:none;color:#ff5b45}a:hover{color:#13334d}a:visited{color:#02b8cd}a:active{color:#f00}\n</style>\n</head>\n<body>\n<ul class='title'>\n<li>"""+RStableName+"""</li>\n""")
	prevIP = ""
	for row in resultList:
		rowStr = ""
		if (row[1]!=prevIP):
			rowStr = "</ul>\n<ul>\n<li><span class='rowid'>"+str(row[0])+"</span><span class='rowip'>"+str(row[1])+"</span><span class='rowtext'><a href='"+row[2]+"' target='_blank'>"+row[2]+"</a> -- "+cgi.escape(row[3])+"</span></li>\n"
		else:
			rowStr = "<li><span class='rowid'>"+str(row[0])+"</span><span class='rowip'>&nbsp;</span><span class='rowtext'><a href='"+row[2]+"' target='_blank'>"+row[2]+"</a> -- "+cgi.escape(row[3])+"</span></li>\n"
		prevIP = row[1]
		html.write(rowStr)

	html.write("</ul>\n</body>\n</html>")
	return filePath+fileName

